package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.pojo.*;
import com.ssm.service.StuMistakeService;
import com.ssm.service.StuService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class StuController {

    @Autowired
    StuService stuService;

    @Autowired
    StuMistakeService stuMistakeService;

    @RequestMapping(value = "/welcome")
    public String query(Model model, String number) {
        Stu stu = stuService.query(number);
        model.addAttribute("stu", stu);
        return "index";
    }

    @RequestMapping(value = "/showStu", method = RequestMethod.GET)
    @ResponseBody
    public Msg showStu(@RequestParam(value = "pn", defaultValue = "1") int pn,
                        @RequestParam(value = "select_value", defaultValue = "1") int value,
                        @RequestParam(required = false) String find_value,@RequestParam("cdorm")String cdorm) {
        /*
         * 在查询之前只需要调用，传入页码。以及每页的大小
         * */
        if (find_value == "undefined") {
            find_value = "";
        }
        System.out.println("--------------------------------------"+cdorm);
        PageHelper.startPage(pn, 5);
        if (value == 1) {
            System.out.println("--------------------------------------"+cdorm);
            List<StuMistakeData> stuMistakeData = stuService.queryAllCdorm(cdorm);
            System.out.println("---1234564----------------------stuMistakeData----------"+stuMistakeData);

            PageInfo page = new PageInfo(stuMistakeData, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 2) {
            List<StuMistakeData> list2 = stuService.find_name_cdorm(find_value,cdorm);
            PageInfo page = new PageInfo(list2);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else {
            return Msg.fail().add("value", value);
        }
    }
    @RequestMapping(value = "/showStud", method = RequestMethod.GET)
    @ResponseBody
    public Msg showStud(@RequestParam(value = "pn", defaultValue = "1") int pn,
                        @RequestParam(value = "select_value", defaultValue = "1") int value,
                        @RequestParam(required = false) String find_value,
                        @RequestParam(value = "cdorm",required = false)String cdorm) {
        /*
         * 在查询之前只需要调用，传入页码。以及每页的大小
         * */
        if (find_value == "undefined") {
            find_value = "";
        }
        PageHelper.startPage(pn, 5);
        if (value == 1) {
            List<StuMistake> list = stuMistakeService.listAll(cdorm);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 2) {
            List<StuMistake> list = stuMistakeService.find_name_cdormd(cdorm,find_value);
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 3) {
            List<StuMistake> list = stuMistakeService.find_name_cdormc(cdorm);
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        }else if (value == 4) {
            List<StuMistake> list = stuMistakeService.findNotStatu();
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else {
            return Msg.fail().add("value", value);
        }
    }

    @RequestMapping(value = "/showStus", method = RequestMethod.GET)
    @ResponseBody
    public Msg showStus(@RequestParam(value = "pn", defaultValue = "1") int pn,
                        @RequestParam(value = "select_value", defaultValue = "1") int value,
                        @RequestParam(required = false) String find_value,
                        @RequestParam(value = "cdorm",required = false)String cdorm) {
        /*
         * 在查询之前只需要调用，传入页码。以及每页的大小
         * */
        if (find_value == "undefined") {
            find_value = "";
        }
        PageHelper.startPage(pn, 5);
        if (value == 1) {
            List<Stu> list = stuService.queryAll(cdorm);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 2) {
            List<Stu> list = stuService.find_name(find_value,cdorm);
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 3) {
            List<Stu> list = stuService.find_cdorm(find_value);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else {
            return Msg.fail().add("value", value);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getStu", method = RequestMethod.POST)
    public Msg getStu(String number) {
        Stu stu = stuService.query(number);
        return Msg.success().add("stu", stu);
    }

    @ResponseBody
    @RequestMapping(value = "/getStud", method = RequestMethod.GET)
    public Msg getStud(String number,String cdorm) {
        Stu stu = null;
        System.out.println("------------------------------------"+number);
        try {
            stu = stuService.query(number);
            if(stu==null){
                return Msg.fail().add("msg","该学生不存在！");
            }else if(!stu.getCdorm().equals(cdorm)){
                return Msg.fail().add("msg","该学生是"+stu.getCdorm()+"栋！");
            }
        } catch (Exception e) {
            return Msg.fail().add("msg","系统异常,请上报！");
        }
        return Msg.success().add("stu", stu);
    }

    @ResponseBody
    @RequestMapping(value = "/getStuMistake", method = RequestMethod.GET)
    public Msg getStuMistake(String number) {
        StuMistake stu = stuMistakeService.query(number);
        return Msg.success().add("stu", stu);
    }
    @ResponseBody
    @RequestMapping(value = "/updateStatu",method = RequestMethod.GET)
    public Msg updateStatu(String number,int statu){
        try {
            stuMistakeService.updateStatu(number,statu);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","操作失败");
        }
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/checkStu", method = RequestMethod.POST)
    public Msg checkStu(String number) {
        String regNumber = "(^[0-9]{4,14}$)";
        if (!number.matches(regNumber)) {
            return Msg.fail().add("va_msg", "学号应为4~14位数字");
        }
        Stu i = stuService.query(number);
        System.out.println("==================================================" + i);
        int i1 = i == null ? 0 : 1;
        if (i1 != 1) {
            return Msg.success();
        } else {
            return Msg.fail().add("va_msg", "学号存在");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/stuUpdate", method = RequestMethod.POST)
    public Msg stuUpdate(Stu stu) {
        System.out.println(stu);
        if (stu.getSphone() == null) {
            stu.setSphone("");
        }
        if (stu.getCdorm() == null) {
            stu.setCdorm("");
        }
        if (stu.getSdorm() == null) {
            stu.setSdorm("");
        }
        try {
            stuService.stuUpdate(stu);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail().add("msg","操作失败");
        }
        StuMistake stuMistake = stuMistakeService.query(stu.getNumber());
        if(stuMistake!=null){
            if(stuMistake.getSname()!=stu.getSname()){
                stuMistake.setSname(stu.getSname());
                stuMistakeService.updateByNumber(stuMistake);
            }
        }
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/stuMistakeUpdate", method = RequestMethod.GET)
    public Msg stuMistakeUpdate(StuMistake stu) {
        System.out.println(stu);
        stuMistakeService.stuMistakeUpdate(stu);
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/saveStu", method = RequestMethod.POST)
    public Msg saveStu(@Valid Stu stu, BindingResult result) { //@Valid代表manager的数据要被校验,BindingResult类为封装校验的结果
        if (stu.getSphone() == null) {
            stu.setSphone("");
        }
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<String, Object>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError error : fieldErrors) {
                map.put(error.getField(), error.getDefaultMessage());
            }
            return Msg.fail().add("errorFields", map);
        } else {
            stuService.addStu(stu);
            return Msg.success();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/saveStuMistake", method = RequestMethod.GET)
    public Msg saveStuMistake(StuMistake stuMistake) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(date);
        stuMistake.setCreateTime(format1);
        System.out.println("-----------------------------------"+stuMistake);
        try {
            stuMistakeService.addStuMistake(stuMistake);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/stuDel", method = RequestMethod.POST)
    public Msg stuDel(String number) {
        if (number.contains("-")) {
            List<String> list = new ArrayList<String>();
            String[] strs = number.split("-");
            for (String str : strs) {
                list.add(str);
            }
            try {
                stuService.deleteStuList(list);
            } catch (Exception e) {
                e.printStackTrace();
                return Msg.fail();
            }
            stuMistakeService.deleteStuList(list);
            return Msg.success();
        } else {
            try {
                stuService.deleteStu(number);
            } catch (Exception e){
                e.printStackTrace();
                return Msg.fail();
            }
            stuMistakeService.deleteByNumber(number);
            return Msg.success();
        }
    }
    @ResponseBody
    @RequestMapping(value = "/stuMistakeDel", method = RequestMethod.GET)
    public Msg stuMistakeDel(String number) {

        try {
            stuMistakeService.deleteByNumber(number);
        } catch (Exception e) {
            return Msg.fail().add("msg","操作错误");
        }
        return Msg.success();
    }
}
